'use strict';

/* Controllers */

angular.module('app')
    .controller('AppCtrl', ['$scope', '$translate','$rootScope', '$localStorage', '$window','$http',
        function($scope, $translate,$rootScope, $localStorage, $window,$http) {
            // add 'ie' classes to html
            var isIE = !!navigator.userAgent.match(/MSIE/i);
            isIE && angular.element($window.document.body).addClass('ie');
            isSmartDevice($window) && angular.element($window.document.body).addClass('smart');

            $scope.searchIssue = {
                value: "",
            };

            //点击全部类型
            $scope.issueClick = function(issue) {
                $scope.crtIssue = !issue ? "" : issue.value;
                $rootScope.initializationSearch($scope.crtIssue);
            };

            //点击搜索按钮查询
            $scope.searchClick = function() {
                console.log("点击搜索按钮");
                $rootScope.initializationSearch($scope.searchIssue.value);
            };

            $scope.header = {
                showStatus : true
            };

            // $scope.assignUser = {};
            // $scope.selectAssignUser = function(user) {
            //     $rootScope.initializationSearch(user.account);
            // };

            $.ajax({
                type: "get",
                url: sys_config_url + "/select/get?type=BRD_BUSINESS",
                async: false,
                success: function (data) {
                    $scope.business=data.data.BRD_BUSINESS
                }
            });
            $rootScope.search={}

            // config
            $scope.app = {
                name: 'Matrix',
                version: '1.0.1',
                // userName: getUserName(),
                // for chart colors
                color: {
                    primary: '#7266ba',
                    info: '#23b7e5',
                    success: '#27c24c',
                    warning: '#fad733',
                    danger: '#f05050',
                    light: '#e8eff0',
                    dark: '#3a3f51',
                    black: '#1c2b36'
                },
                settings: {
                    themeID: 1,
                    navbarHeaderColor: 'bg-black',
                    navbarCollapseColor: 'bg-white-only',
                    asideColor: 'bg-black',
                    headerFixed: true,
                    asideFixed: true,
                    asideFolded: false,
                    asideDock: false,
                    container: false
                },
                headerPanel: {
                    buttons: [], // tilte, func
                    message: null,
                    search: {
                        show: true
                    }
                },
                searchBtn: "",
                issueShow: true,
                page: {
                    name: "",
                    saveShow: false,
                }
            }
            
            $rootScope.initializationSearch = function(value){
                $("#matrixHeaderFilter").val(value).keyup();
            }

            //设置header顶部组件(下拉框，查询框，点击按钮)
            $scope.headerElemets = {
                searchEle: {
                    show : false,
                    searchValue: "",
                },
                sltTree  : {
                    show : false,
                },
                selectEle: [],
                uiSltEle : [],
                btnEle   : [],
            };

            $scope.settings = {
                searchEle: {
                    show : false,
                    searchValue: "",
                },
                sltTree  : {
                    show : false,
                },
                selectEle: [],
                uiSltEle : [],
                btnEle   : [],
                switchs   : []
            };

             //点击业务线下拉
            $scope.onBusines=function(y){
                if(null==y){
                    y={};
                    y.key=-1
                    y.value="所有业务线"
                }
                $rootScope.search=y;
                if(y.key!=-1){
                    $scope.app.headerPanel.search.keyword = y.value;
                }else{
                    $scope.app.headerPanel.search.keyword = "";
                }
                $scope.initializationSearch($scope.app.headerPanel.search.keyword);
            }
            $scope.onBusines(null)


            // save settings to local storage
            if (angular.isDefined($localStorage.settings)) {
                $scope.app.settings = $localStorage.settings;
            } else {
                $localStorage.settings = $scope.app.settings;
            }
            $scope.$watch('app.settings', function() {
                if ($scope.app.settings.asideDock && $scope.app.settings.asideFixed) {
                    // aside dock and fixed must set the header fixed.
                    $scope.app.settings.headerFixed = true;
                }
                // for box layout, add background image
                $scope.app.settings.container ? angular.element('html').addClass('bg') : angular.element('html').removeClass('bg');
                // save to local storage
                $localStorage.settings = $scope.app.settings;
            }, true);

            // angular translate
            $scope.lang = {
                isopen: false
            };
            $scope.langs = {
                en: 'English',
                de_DE: 'German',
                it_IT: 'Italian'
            };
            $scope.selectLang = $scope.langs[$translate.proposedLanguage()] || "English";
            $scope.setLang = function(langKey, $event) {
                // set the current lang
                $scope.selectLang = $scope.langs[langKey];
                // You can change the language during runtime
                $translate.use(langKey);
                $scope.lang.isopen = !$scope.lang.isopen;
            };

            $scope.$on('$stateChangeStart', function (e, toState, toParams, fromState, fromParams) {
                $rootScope.clean($scope.app);
                $rootScope.initHeaderElements($scope.headerElemets);
            });

            function isSmartDevice($window) {
                // Adapted from http://www.detectmobilebrowsers.com
                var ua = $window['navigator']['userAgent'] || $window['navigator']['vendor'] || $window['opera'];
                // Checks for iOs, Android, Blackberry, Opera Mini, and Windows mobile devices
                return (/iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/).test(ua);
            }

            $scope.breadcrumb = [];

        }
    ]);